﻿using MyInspiration.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace MyInspiration.Controllers
{
    public class PersonalController : Controller
    {
        MyIdeaEntities my = new MyIdeaEntities();
        //
        // GET: /Personal/
        /// <summary>
        /// 个人主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int id = 1)
        {
            //登录成功跳转到该页面
            //页面显示的数据为QQ登录用户的灵感
            //得到当前登录用户的QQ号
            string QQ = Session["uid"]!=null?Session["uid"].ToString():"";
            //根据QQ号去查当前用户的灵感
            List<IdeaList> list = my.IdeaList.Where(m => m.idea_ByQQ == QQ).ToList();


            return View(list.OrderByDescending(m => m.idea_Create_Time).ToPagedList<IdeaList>(id, 4));


        }
        public ActionResult Add_Personal_Inspiration() 
        {
            return PartialView();
        }
        /// <summary>
        /// 添加个人灵感
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        [ValidateAntiForgeryTokenOnAllPosts]
        public ActionResult Add(IdeaList i)
        {
            if (!Request.IsAjaxRequest()) return Content("<script>while(true)console.log(1);</script>");
            if (string.IsNullOrEmpty(i.idea_Publisher) || i.idea_Publisher.Trim() == "")
            {
                i.idea_Publisher = "匿名";
            }
            
            i.idea_Status = 1;//0：为草稿 1：发布 (暂如此设计)
            i.idea_Create_Time = DateTime.Now;
            my.IdeaList.Add(i);
            my.SaveChanges();

            var jsonObj = new { res = "Success", info = "成功" };
            return Json(jsonObj, JsonRequestBehavior.AllowGet);

        }
    }
}
